<!--生产商-->
<template>
  <div>
    <el-select v-model="firmId" :placeholder="placeholder" clearable filterable style="width: 100%;"
               @change="producerChange">
      <el-option v-for="item in producerList" :key="item.supplierId" :label="item.supplierName" :value="item.supplierId">
      </el-option>
    </el-select>
  </div>
</template>

<script lang="ts" setup>

import {listMaterialSupplier} from '@/api/material/materialSupplier'
import {useUserStoreHook} from "@/store/modules/user";

const useUserStore = useUserStoreHook();
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo;
// 厂商list
const producerList = ref<any>([])
const props = defineProps({
  placeholder: {
    type: String,
    default: "请选择"
  },
  // 厂商id
  supplierId: {
    type: String,
    default: ''
  }
})

const firmId = ref(props.supplierId)

watch(() => props.supplierId, (newValue, _oldValue) => {
  firmId.value = newValue
})
const emits = defineEmits(["producerChange"])

getProducerList()

// 获取厂商list
function getProducerList() {
  const param = {delFlag: '0', supplierClass: '0', usedFlag: 'Y',orgId: userInfo.orgId}
  listMaterialSupplier(param).then(res => {
    producerList.value = res.data
  })
}

function producerChange(value) {
  let producerInfo = {}
  let index = producerList.value.findIndex(item => {
    return item.supplierId === value
  })
  if (index > -1) {
    producerInfo = producerList.value[index]
  }
  emits("producerChange", producerInfo)
}
</script>

<script lang="ts">
export default {
  name: "producer"
}
</script>

<style scoped></style>
